// 
// $Rev:: 22            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-25 1#$:  Date of last commit
// 
package agents.agents;

import com.ibm.aglet.*;
import com.ibm.agletx.util.SeqPlanItinerary;

import java.net.URL;

import agents.aglets.AgletBridge;
import agents.interfaces.IMessage;


/**
 * Agents that can navigate through the Travel Agent System.
 * 
 * This class represents the type of agents that can move from host to host,
 * searching or performing tasks given to it by stationary agents.
 * 
 * @author Ruben Van Havermaet
 * @version 0.2
 */
public class  MobileServiceAgent extends ServiceAgent
{

    /** An URL, specifies the agent's home-address. */
    protected URL _home = null;

    
//    protected AgletID _homeUI = null;

    /**
     * A SeqPlanItinerary that helps the agent to navigate through the system.
     */
    protected SeqPlanItinerary itinerary_ = null;

    /**
     * An initialisation method, called when the agent is created.
     * 
     * This method will initialise the home_ URL and the homeUI_ AgletID, it
     * will add a MobileAgentListener to this type of agent and it will set up
     * the itinerary_.
     * 
     * @param init
     *            an Initialising Object
     * @see agents.agents.MobileAgentListener
     */
    public void onCreation(Object init)
    {
	super.onCreation(init);
	_home = AgletBridge.getCurrentLocation(this);
//	_homeUI = AgletBridge.getAgletID(_uiAgent);
	// TODO: create my own mobility listener
//	addMobilityListener(new MobileAgentListener(this.getProxy()));
	itinerary_ = new SeqPlanItinerary(this);
    }

    /**
     * Let the agent start its trip.
     * 
     */
    public void start()
    {
	itinerary_.startTrip();
    }

    // *** PUBLIC MESSAGE METHODS *********************************************
//    /**
//     * Install the Receptionist for this MobileServiceAgent.
//     * 
//     * When mobile agents arrive at a new location they must receive an
//     * AgletProxy for the Receptionist that is running at the host at which they
//     * have just arrived. The SetReceptionistMessage is sent from the ServiceApp
//     * that is listening to certain ContextEvents, like the arrival of new
//     * agents. When the Receptionist is properly installed, the mobile agent can
//     * ask it for an AgletProxy representing the current agent responsible for
//     * the UI.
//     * 
//     * @param msg
//     *            a SetReceptionistMessage
//     * @see agents.server.ServiceApp#agletArrived
//     * @see agents.msgs.SetReceptionistMessage
//     */
//    public void setReceptionist(Message msg)
//    {
//	AgletProxy receptionist = (AgletProxy) msg.getArg();
//	currentReceptionist_ = receptionist;
//	setUIAgent();
//    }

    /**
     * Initialise the beginning itinerary.
     * 
     * This method makes sure the creator of this search agent can set the
     * itinerary properly. For instance, for SearchAgents, the TASInteractiveGUI
     * sets this itinerary_ to point to the Main Office.
     * 
     * @param msg
     *            a Message
     * @see agents.msgs.SetInitPlanMessage
     */
    public void addAction(IMessage message, String host)
    {
	itinerary_.addPlan(host, message.getMessage());
    }

    /**
     * Through this method, a SearchAgent can send the UI-agent at home, text to
     * display.
     * 
     * @param msg
     *            a ReportHomeMessage
     * @see agents.msgs.ReportHomeMessage
     */
    public void reportHome(Message msg)
    {
//	if (_home == null | _homeUI == null)
//	{
//	    System.out.println("Cannot reach home: this feature requires you"
//		    + " to initialise home_ and homeUI_ onCreation.");
//	    return;
//	}
//	AgletContext cxt = AgletBridge.getCurrentContext(this);
//	AgletProxy homeUIAgent = AgletBridge.getAgletProxy(cxt, _home, _homeUI);
//	PrintMessage m = MessageFactory
//		.newPrintMessage(msg.getArg().toString());
//	AgletBridge.sendOnewayMessage(homeUIAgent, m);
    }

    @Override
    public void receive(IMessage message)
    {
	message.sentTo(this);	
    }
}
